﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;


namespace MvcXRepairAdmin.Repository.SQL
{
    public class SymptomDAO : BaseDAO
    {
        public IEnumerable<Symptom> GetAllActiveSymptoms()
        {
            string spName = "GetAllActiveSymptoms";
            
            DataTable resultTab = null;            
            List<Symptom> symptomList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }

            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                symptomList = this.ConvertDataTableToSymptomList(resultTab);
            }

            return symptomList;            
        }

        public bool DeactivateSymptomByID(int id)
        {
            string spName = "DeactiveSymptomByID";

            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", id);
                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool AddNewSymptom(Symptom symptom)
        {
            string spName = "AddNewSymptom";
            object newIdentity = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ModelID", symptom.ModelID);
                command.AddInParameter("BrandID", symptom.BrandID);                
                command.AddInParameter("Name", symptom.Name);
                command.AddInParameter("Description", symptom.Description);
               
                command.AddOutParameter("NewIdentity", -1);
                command.ExecuteNonQuery();

                newIdentity = command.GetParameterValue("NewIdentity");
            }

            long newIdentityValue;

            if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool UpdateSymptom(Symptom symptom)
        {
            string spName = "UpdateSymptom";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;       

                command.AddInParameter("ID", symptom.ID);
                command.AddInParameter("ModelID", symptom.ModelID);
                command.AddInParameter("BrandID", symptom.BrandID);
                command.AddInParameter("Name", symptom.Name);
                command.AddInParameter("Description", symptom.Description);
                command.AddInParameter("IsActive", symptom.IsActive);

                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        private List<Symptom> ConvertDataTableToSymptomList(DataTable data)
        {
            List<Symptom> symptoms = null;

            if (data != null)
            {
                symptoms = new List<Symptom>();

                foreach (DataRow dr in data.Rows)
                {
                    var symptomModel = new Symptom();

                    symptomModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    symptomModel.Name = SqlUtil<string>.GetValue(dr, "Name");
                    symptomModel.Description = SqlUtil<string>.GetValue(dr, "Description");
                    symptomModel.BrandID = SqlUtil<int>.GetValue(dr, "BrandID");
                    symptomModel.ModelID = SqlUtil<int>.GetValue(dr, "ModelID");

                    symptoms.Add(symptomModel);
                }
            }

            return symptoms;
        }
    }
}
